package solution

import (
	"container/list"
	"myleecode/solution/common"
)

// 20. 有效括号

func Leecode20_IsValid(s string) bool {
	stack := list.New()
	for _, c := range s {
		if stack.Len() == 0 {
			stack.PushFront(c)
			continue
		}
		e := stack.Front()
		v := e.Value.(int32)
		if v == '(' && c == ')' {
			stack.Remove(e)
		}
		if v == '[' && c == ']' {
			stack.Remove(e)
		}
		if v == '{' && c == '}' {
			stack.Remove(e)
		}
	}
	return stack.Len() == 0
}

// 21. 合并两个有序链表
func Leecode21_MergeTwoLists(l1 *common.ListNode, l2 *common.ListNode) *common.ListNode {
	if l1 == nil {
		return l2
	}
	if l2 == nil {
		return l1
	}
	if l1.Val < l2.Val {
		l1.Next = Leecode21_MergeTwoLists(l1.Next, l2)
		return l1
	}
	l2.Next = Leecode21_MergeTwoLists(l1, l2.Next)
	return l2
}
